package helipay.api.web.controller;

import java.util.HashMap;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import helipay.api.web.view.ResultVO;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.dao.DuplicateKeyException;
import org.springframework.web.servlet.HandlerExceptionResolver;
import org.springframework.web.servlet.ModelAndView;

import helipay.api.common.exception.BaseAPIException;
import helipay.api.common.exception.ExceptionCodeStatusEnum;
import helipay.api.util.I18nUtil;
import helipay.api.util.Json;

/**
 * 所有spring 抛出的异常捕捉类
 * 
 */
public class WebHandlerExceptionResolver implements
		HandlerExceptionResolver {
	public static String ERROR_CODE_PARAMETER_NAME = "errorCode";
	public static String ERROR_MESSAGE_PARAMETER_NAME = "errorMessage";
	public static String ERROR_MESSAGE_LANG_CODE = "lang";
	private final static Log logger = LogFactory
			.getLog(WebHandlerExceptionResolver.class);

	public ModelAndView resolveException(HttpServletRequest request,
			HttpServletResponse response, Object handler, Exception exception) {
		String viewName = WebHandlerExceptionResolver.getErrorInfo(request, response, exception);

		String errorCode =  (String) request.getAttribute(ERROR_CODE_PARAMETER_NAME);
		String errorMessage =  (String) request.getAttribute(ERROR_MESSAGE_PARAMETER_NAME);
		
		ModelAndView view = new ModelAndView(viewName);		

		ResultVO<Object> result = new ResultVO<Object>();
		
		result.setErrorCode(errorCode);
		result.setErrorMessage(errorMessage);
		result.setResult(new HashMap<String,String>());
		
		view.addObject("message", Json.toJson(result));
		
		return view;
	}

	public static void log(HttpServletRequest request) {
		Object obj = request.getAttribute("message");
		if (obj == null) {
			return;
		}
		String message = obj.toString();
		String url = request.getRequestURL().toString();
		if (message.startsWith("{")) {
			Exception e = new Exception("json url:" + url + " data:"
					+ StringUtils.substring(message, 50));
			logger.error(message, e);
		}
	}

	public static String getErrorInfo(HttpServletRequest request, HttpServletResponse response, Throwable exception) {
		String lang = request.getAttribute(ERROR_MESSAGE_LANG_CODE)!=null?(String) request.getAttribute(ERROR_MESSAGE_LANG_CODE):"zh_CN";
		logger.info("exception:" + exception.getMessage(), exception);

		request.setAttribute(ERROR_CODE_PARAMETER_NAME, ExceptionCodeStatusEnum.FAILED_STATUS.toString());
		request.setAttribute(ERROR_MESSAGE_PARAMETER_NAME, I18nUtil.getMessage(ExceptionCodeStatusEnum.FAILED_STATUS.toString(), null, lang));
		//request.setAttribute(ERROR_MESSAGE_PARAMETER_NAME, exception.getMessage());
		
		String errorCode = ExceptionCodeStatusEnum.FAILED_STATUS.toString();
		if (exception instanceof BaseAPIException) {
			errorCode = ((BaseAPIException) exception).getErrorCode();
			request.setAttribute(ERROR_CODE_PARAMETER_NAME, errorCode);
			request.setAttribute(ERROR_MESSAGE_PARAMETER_NAME, exception.getMessage());
		}
		if(exception instanceof IllegalArgumentException){
			errorCode=ExceptionCodeStatusEnum.ILLEGAL_ARGUMENT.toString();
			request.setAttribute(ERROR_CODE_PARAMETER_NAME, errorCode);
			request.setAttribute(ERROR_MESSAGE_PARAMETER_NAME, exception.getMessage());
		}
//		if(exception instanceof DuplicateKeyException){
//			request.setAttribute(ERROR_CODE_PARAMETER_NAME, ExceptionCodeStatusEnum.FAILED_STATUS.toString());
//			request.setAttribute(ERROR_MESSAGE_PARAMETER_NAME, I18nUtil.getMessage(ExceptionCodeStatusEnum.FAILED_STATUS.toString(), null, lang));
//		}
		
		response.setStatus(HttpServletResponse.SC_OK);
		
        //logger.error(getMessage(exception),exception);
        
		return "message.json";

	}
}
